﻿using System;
using System.Collections;

namespace ExpressesClass.BestApi.Utils.jsonUtil.iterator
{
    class StringCharacterIterator : IEnumerator
    {

        private string text;
        private int begin;
        private int end;
        // invariant: begin <= pos <= end
        private int pos;

        public static char DONE = '\uFFFF';

        public StringCharacterIterator(string text, int begin, int end, int pos)
        {
            if (text == null)
                throw new Exception();
            this.text = text;

            if (begin < 0 || begin > end || end > text.Length)
                throw new Exception("Invalid substring range");

            if (pos < begin || pos > end)
                throw new Exception("Invalid position");

            this.begin = begin;
            this.end = end;
            this.pos = pos;
        }

        public void setText(string text)
        {
            if (text == null)
                throw new Exception();
            this.text = text;
            this.begin = 0;
            this.end = text.Length;
            this.pos = 0;
        }


        public char first()
        {
            pos = begin;
            return Convert.ToChar(Current);
        }

        public char last()
        {
            if (end != begin)
            {
                pos = end - 1;
            }
            else
            {
                pos = end;
            }
            return Convert.ToChar(Current);
        }

        public char setIndex(int p)
        {
            if (p < begin || p > end)
                throw new Exception("Invalid index");
            pos = p;
            return Convert.ToChar(Current);
        }


        public object Current
        {
            get
            {
                try {

                    if (pos >= begin && pos < end)
                    {
                        return char.Parse(text.Substring(pos, 1));
                    }
                    else
                    {
                        return DONE;
                    }

                } catch (IndexOutOfRangeException) {
                    throw new InvalidOperationException();
                }
            }
        }

        public char next()
        {
            if (pos != text.Length - 1)
            {
                pos++;
                char c = char.Parse(text.Substring(pos, 1));
                return c;
            }
            else
            {
                pos = text.Length;
                return DONE;
            }
        }

        public char previous()
        {
            if (pos > begin)
            {
                pos--;
                return char.Parse(text.Substring(pos, 1));
            }
            else
            {
                return DONE;
            }
        }

        public bool MoveNext()
        {
            if (pos != text.Length) //判断当前位置是否为最后一个，如果不是游标自增 #4
            {
                pos++;
            }
            return pos < text.Length;
        }

        public void Reset()
        {
            pos = -1;
        }

        public int getIndex()
        {
            return pos;
        }

        public int getEndIndex()
        {
            return end;
        }

        public int getBeginIndex()
        {
            return begin;
        }
    }
}
